package com.huixuebao.exchange.service.Impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huixuebao.common.bean.ApiResult;
import com.huixuebao.common.enumeration.ResultCodeEnum;
import com.huixuebao.common.util.ConvertUtils;
import com.huixuebao.exchange.dto.*;
import com.huixuebao.exchange.entity.MechanismEntity;
import com.huixuebao.exchange.mapper.MechanismMapper;
import com.huixuebao.exchange.service.MechanismService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Slf4j
@Service
public class MechanismServiceImpl implements MechanismService {

    @Autowired
    MechanismMapper mechanismMapper;

    /**
     * 获取放心学合作机构
     * @param current
     * @param size
     * @param cityIds
     * @param merchantsName
     * @param merchantsState
     * @param contactsName
     * @param contactsPhone
     * @return
     */
    @Override
    public ApiResult<List<MechanismDTO>> getRestAssuredMechanism(Integer current, Integer size, String cityIds, String merchantsName, String merchantsState, String contactsName,
                                                                 String contactsPhone) {
        ApiResult apiResult = new ApiResult();
        try{
            if (ObjectUtil.isNotNull(current) && ObjectUtil.isNotNull(size)) {
                //通过时间创建时间排序
                LambdaQueryWrapper<MechanismEntity> lambdaQuery = new LambdaQueryWrapper<MechanismEntity>().eq(MechanismEntity::getIsCollaborate,1);
                IPage<MechanismEntity> page = new Page<>(current,size);
                lambdaQuery.like(StrUtil.isNotBlank(merchantsName), MechanismEntity::getName, merchantsName)
                        .like(StrUtil.isNotBlank(merchantsState), MechanismEntity::getState, merchantsState)
                        .like(StrUtil.isNotBlank(contactsName),MechanismEntity::getContactsName,contactsName)
                        .like(StrUtil.isNotBlank(contactsPhone),MechanismEntity::getContactsPhone,contactsPhone);
                IPage<MechanismEntity> entityPage = mechanismMapper.selectPage(page, lambdaQuery);

                IPage<MechanismDTO> dtoPage = ConvertUtils.convertToDTOPage(entityPage, MechanismDTO.class);
                apiResult.setData(dtoPage);
                apiResult.setSuccess(true);
                apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                apiResult.setMessage("获取放心学分页商户成功!!!");
            }else {
                //返回没有分页列表信息，没有任何条件查询
                List<MechanismEntity> list = mechanismMapper.selectList(new LambdaQueryWrapper<MechanismEntity>().eq(MechanismEntity::getIsCollaborate,1));
                List<MechanismDTO> dtoList = ConvertUtils.convertToDTOList(list, MechanismDTO.class);
                apiResult.setData(dtoList);
                apiResult.setSuccess(true);
                apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                apiResult.setMessage("获取放心学列表商户成功!!");
            }
        }catch (Exception e){
            apiResult.setSuccess(false);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("获取放心学列表失败!!");
        }
        return apiResult;
    }

    @Override
    public ApiResult getMechanismItem(String id) {
        ApiResult apiResult = new ApiResult();
        try{
            MechanismEntity mechanism = mechanismMapper.selectById(id);
            MechanismAttributeDTO mechanismAttributeDTO = ConvertUtils.convertToDTO(mechanism,MechanismAttributeDTO.class);
            MechanismItemDTO mechanismItemDTO = new MechanismItemDTO();
            mechanismItemDTO.setData(mechanismAttributeDTO);
            apiResult.setData(mechanismItemDTO);
            apiResult.setSuccess(true);
            apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
            apiResult.setMessage("获取机构信息成功!!");
        }catch (Exception e){
            log.info("获取机构信息异常！！");
            apiResult.setSuccess(false);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("获取机构信息失败!!");
        }
        return apiResult;
    }

    @Override
    public ApiResult<List<MechanismDTO>> getMechanismList(Integer current, Integer size) {
        ApiResult apiResult = new ApiResult();
        try {
            if (ObjectUtil.isNotNull(current) && ObjectUtil.isNotNull(size)){
                IPage<MechanismEntity> page = new Page<>(current,size);
                LambdaQueryWrapper<MechanismEntity> lambdaQuery = new LambdaQueryWrapper<MechanismEntity>().orderByDesc(MechanismEntity::getCreateTime);
                IPage<MechanismEntity> entityPage = mechanismMapper.selectPage(page, lambdaQuery);
                IPage<MechanismNameDTO> dtoPage = ConvertUtils.convertToDTOPage(entityPage,MechanismNameDTO.class);
                apiResult.setData(dtoPage);
                apiResult.setSuccess(true);
                apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                apiResult.setMessage("获取机构分页信息成功！！");
            }else {
                List<MechanismEntity> list = mechanismMapper.selectList(null);
                List<MechanismNameDTO> dtoList = ConvertUtils.convertToDTOList(list, MechanismNameDTO.class);
                apiResult.setData(dtoList);
                apiResult.setSuccess(true);
                apiResult.setCode(ResultCodeEnum.SUCCESS.getCode());
                apiResult.setMessage("获取机构列表信息成功!!");
            }
        }catch (Exception e){
            log.info("获取机构信息异常");
            apiResult.setSuccess(true);
            apiResult.setCode(ResultCodeEnum.FAILURE.getCode());
            apiResult.setMessage("获取机构列表信息失败!!");
        }
        return apiResult;
    }

    @Override
    public ApiResult addMechanismPolicyNews(MechanismPolicyDTO mechanismPolicyDTO) {
        return null;
    }
}
